//
//  XHCSJFeedViewController.m
//  XHAdSDKTest
//
//  Created by 昱胜 on 2022/7/21.
//

#import "XHCSJFeedViewController.h"

//广告头文件
#import <XHAdSDK/XHAdSDK.h>

@interface XHCSJFeedViewController ()<UITableViewDataSource, UITableViewDelegate,XHNativeAdDataDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation XHCSJFeedViewController

- (void)dealloc {
    _tableView.delegate = nil;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
//    self.haveRenderSwitchView = YES;
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self loadNativeAds];
    
   
    [self.tableView reloadData];
}

- (void)loadNativeAds {
    XHAdCenter * center = [XHAdCenter new];
    XHNativeAdData * data = [XHNativeAdData new];
    data.rootViewController = self;
    data.nativeAdDelegate = self;
    data.adSize = CGSizeMake(self.view.frame.size.width, 0);
    data.adCount = 10;
    __weak typeof(self) weakSelf = self;
    //使用289121257167781981
    [center xh_showNativeAd:data appId:@"289121257167781981" complate:^(NSArray * _Nullable adViewListAry) {
        if (adViewListAry.count <= 0) {
            return;
        }
        for (UIView *view in adViewListAry) {
            NSLog(@"view.frame ==> %@",NSStringFromCGRect(view.frame));
        }
        self.dataSource = [NSMutableArray arrayWithArray:adViewListAry];
        [weakSelf.tableView reloadData];
    }];
    
    
//    XHAdCenter * center1 = [XHAdCenter new];
//    XHNativeAdData * data1 = [XHNativeAdData new];
//    data1.rootViewController = self;
//    data1.nativeAdDelegate = self;
//    data1.adSize = CGSizeMake(self.view.frame.size.width/2, 0);
//    data1.adCount = 10;
//    //使用
//    [center1 xh_showNativeAd:data1 appId:@"356804558841708641" complate:^(NSArray * _Nullable adViewListAry) {
//        NSLog(@"adViewListAry ==>  %@",adViewListAry);
//        if (adViewListAry.count <= 0) {
//            return;
//        }
//        for (UIView *view in adViewListAry) {
//            NSLog(@"view.frame1111111 ==> %@",NSStringFromCGRect(view.frame));
//        }
//    }];
    
}

/** 广告加载失败，msg加载失败说明（如果重新请求广告，注意：只重新请求一次）*/
- (void)xh_onSplashAdFail:(NSString *)errorString {
    
}

/** 广告渲染成功 */
- (void)xh_nativeAdExposure {
    [self.tableView reloadData];
}

/** 广告渲染成功 */
- (void)xh_nativeAdExposureWithAdView:(UIView *)adView {
    NSLog(@"渲染成功");
}

/** 广告渲染失败 */
- (void)xh_nativeAdExposureFailWithAdView:(UIView *)adView {
    NSLog(@"渲染失败");
}

/** 广告点击关闭按钮 */
- (void)xh_nativeAdExposureClickCloseWithAdView:(UIView *)adView {
    NSLog(@"点击关闭按钮");
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    // 适配横竖屏
    CGRect tableVieRect = self.tableView.frame;
    self.tableView.frame = CGRectMake(CGRectGetMinX(tableVieRect), CGRectGetMinY(tableVieRect), size.width, size.height);
    [self.tableView reloadData];
}

#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // For ad cells just as the ad cell provider, for normal cells do whatever you would do.
    NSUInteger index = indexPath.row;
    if (index >= self.dataSource.count) {
        return [UITableViewCell new];
    }
    id model = self.dataSource[index];
    if ([model isKindOfClass:[UIView class]]) {
        
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // 重用BUNativeExpressAdView，先把之前的广告试图取下来，再添加上当前视图
        UIView *subView = (UIView *)[cell.contentView viewWithTag:1000];
        if ([subView superview]) {
            [subView removeFromSuperview];
        }
        
        UIView *view = (UIView *)model;
        view.userInteractionEnabled = YES;
        cell.contentView.userInteractionEnabled = YES;
        view.tag = 1000;
        [cell.contentView addSubview:view];
        return cell;
    }
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = @"测试";
    return cell;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // The ad cell provider knows the height of ad cells based on its configuration
    NSUInteger index = indexPath.row;
    id model = self.dataSource[index];
    if ([model isKindOfClass:[UIView class]]) {
        UIView *view = (UIView *)model;
        NSLog(@"view.bounds.size.height ==> %.2f",view.bounds.size.height);
        return view.bounds.size.height;
    }
    return 80;
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger index = indexPath.row;
    id model = self.dataSource[index];
    if ([model isKindOfClass:[UIView class]]) {
        UIView *view = (UIView *)model;
        NSLog(@"view.bounds.size.height ==> %.2f",view.bounds.size.height);
        return view.bounds.size.height;
    }
    return 80;
}


- (void)pbud_insertIntoDataSourceWithArray:(NSArray *)array {
    if (self.dataSource.count > 3) {
        for (id item in array) {
            NSUInteger index = arc4random() % (self.dataSource.count - 3) + 2;
            [self.dataSource insertObject:item atIndex:index];
        }
    }
}

@end
